package org.eclipse.rally.plugin.views;

import java.util.List;

import org.eclipse.rally.plugin.utils.SWTResourceManager;
import org.eclipse.rally.plugin.webservice.RallyMain;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.rallydev.webservice.v1_12.domain.TestCase;


public class TestCaseTabGUI {
    public static Shell shell;
    public static Group grpTask;
    public static int maxTestCaseVal = 10;
    public static int minTestCaseVal = 1;
    public static int testCasePageNo = 1;
    public static int totaltestCases;
    public static Table table;
    public static List lstTestCase;
    static Text text;
    static Text text_6;
    static CCombo combo5;
    static CCombo combo6;
    public final static Composite parent = null;
    final Display display = Display.getCurrent();
    final Cursor handCursor = new Cursor(display, SWT.CURSOR_HAND);

    public static void main(String[] args) {
        Display display = new Display();
        shell = new Shell(display);
        shell.setMinimumSize(new Point(400, 500));
        shell.setLayout(new FillLayout());

        String id = "DE5731";
        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);

        new TestCaseTabGUI().TestCaseTabData(tabFolder);
        shell.setSize(600, 500);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

    public void TestCaseTabData(final TabFolder tabFolder) {
        TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
        tabItem.setText("Test &Cases");

        Composite composite_1 = new Composite(tabFolder, SWT.BORDER);
        tabItem.setControl(composite_1);
        grpTask = new Group(composite_1, SWT.BORDER);
        grpTask.setBounds(2, 25, 850, 300);
        grpTask.setText("Test Cases");
        grpTask.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));

        {
            {
                Label label = new Label(grpTask, SWT.CENTER);
                label.setText("Views :");
                label.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_WIDGET_DARK_SHADOW));
                label.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label.setBounds(10, 23, 36, 13);
            }

            {
                Label label = new Label(grpTask, SWT.CENTER);
                label.setText("ID");
                label.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                label.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label.setBounds(10, 60, 36, 13);
            }

            {
                Label lblPriority = new Label(grpTask, SWT.NONE);
                lblPriority.setText("Type");
                lblPriority.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                lblPriority.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                lblPriority.setAlignment(SWT.CENTER);
                lblPriority.setBounds(144, 60, 49, 13);
            }

            {
                Label label = new Label(grpTask, SWT.NONE);
                label.setText("Priority");
                label.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                label.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label.setBounds(278, 60, 65, 13);
            }

            text = new Text(grpTask, SWT.BORDER);
            text.setBounds(20, 79, 76, 19);
            combo5 = new CCombo(grpTask, SWT.BORDER);
            combo5.setText("All");
            combo5.setItems(new String[] {
                    "All", "Acceptance", "Functional", "Performance",
                    "Regression", "Usability", "User Interface"
                });
            combo5.setEditable(false);
            combo5.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            combo5.setBounds(144, 79, 88, 21);
            combo6 = new CCombo(grpTask, SWT.BORDER);
            combo6.setText("All");
            combo6.setItems(new String[] {
                    "All", "Useful", "Important", "Critical"
                });
            combo6.setEditable(false);
            combo6.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            combo6.setBounds(278, 79, 88, 21);

            {
                final Label clbl_Filter = new Label(grpTask, SWT.NONE);
                clbl_Filter.setImage(SWTResourceManager.getImage(
                        DashboardGUI.class, "/icons/Search-enabled.gif"));
                clbl_Filter.setBounds(395, 79, 58, 23);
                clbl_Filter.setBackground(SWTResourceManager
        				.getColor(SWT.COLOR_LIST_BACKGROUND));
                clbl_Filter.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseDown(MouseEvent e) {
                            testCasePageNo = 1;
                            text_6.setText(testCasePageNo + "");
                            minTestCaseVal = 1;
                            populateTestCase();
                        }
                    });
                clbl_Filter.setCursor(handCursor);
            }

            {
                Label label = new Label(grpTask, SWT.SEPARATOR |
                        SWT.HORIZONTAL);
                label.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_DARK_BLUE));
                label.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_DARK_BLUE));
                label.setAlignment(SWT.CENTER);
                label.setBounds(10, 104, 446, 2);
            }

            table = new Table(grpTask, SWT.BORDER | SWT.FULL_SELECTION);
            table.setLinesVisible(true);
            table.setHeaderVisible(true);
            table.setBounds(11, 104, 820, 160);

            {
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setWidth(200);
                tableColumn.setText("ID");
            }

            {
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setWidth(205);
                tableColumn.setText("Name");
            }

            {
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setWidth(205);
                tableColumn.setText("Type");
            }

            {
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setWidth(205);
                tableColumn.setText("Owner");
            }

            populateTestCase();

            final CLabel clbl_First = new CLabel(grpTask, SWT.NONE);
            clbl_First.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_First.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-MoveAll.gif"));
            clbl_First.setBounds(470, 270, 50, 17);
            clbl_First.setCursor(handCursor);
            clbl_First.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        int x = totaltestCases % maxTestCaseVal;
                        int y = (totaltestCases / maxTestCaseVal);

                        if (x == 0) {
                            testCasePageNo = y;
                        } else {
                            testCasePageNo = y + 1;
                        }

                        minTestCaseVal = (testCasePageNo * 10) -
                            (maxTestCaseVal - 1);
                        text_6.setText(testCasePageNo + "");

                        populateTestCase();
                    }
                });

            final CLabel clbl_Last = new CLabel(grpTask, SWT.NONE);
            clbl_Last.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Last.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-RemoveAll.gif"));
            clbl_Last.setBounds(300, 270, 50, 17);
            clbl_Last.setCursor(handCursor);
            clbl_Last.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        minTestCaseVal = 1;
                        testCasePageNo = 1;
                        text_6.setText(testCasePageNo + "");

                        populateTestCase();
                    }
                });

            final CLabel clbl_Next = new CLabel(grpTask, SWT.NONE);
            clbl_Next.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Next.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/FF-enabled.gif"));
            clbl_Next.setBounds(425, 270, 50, 17);
            clbl_Next.setCursor(handCursor);
            clbl_Next.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        if (lstTestCase.size() == maxTestCaseVal) {
                            minTestCaseVal += maxTestCaseVal;
                            testCasePageNo++;
                            text_6.setText(testCasePageNo + "");
                        }

                        populateTestCase();

                        // populateDefects();
                    }
                });

            final CLabel clbl_Previous = new CLabel(grpTask, SWT.NONE);
            clbl_Previous.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Previous.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-REW.gif"));
            clbl_Previous.setBounds(360, 270, 50, 17);
            clbl_Previous.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        if (minTestCaseVal > 1) {
                            minTestCaseVal -= maxTestCaseVal;
                            testCasePageNo--;
                            text_6.setText(testCasePageNo + "");
                        }

                        populateTestCase();
                    }
                });
            clbl_Previous.setCursor(handCursor);

            text_6 = new Text(grpTask, SWT.BORDER);
            text_6.setBounds(560, 270, 36, 19);
            text_6.setText(testCasePageNo + "");

            Label label = new Label(grpTask, SWT.NONE);
            label.setBounds(530, 270, 49, 13);
            label.setText("P No");
            label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

            CCombo combo = new CCombo(grpTask, SWT.BORDER);
            combo.setBounds(52, 23, 105, 21);
            combo.setText("Quick Views");
            combo.setItems(new String[] {
                    "All Defects", "High Priority(p1-p3)", "Scheduled Defects",
                    "Unscheduled Defects", "Found During Current Iteration",
                    "Found During Current Release", "Doc or Relase Note",
                    "Passing Test Cases"
                });
            combo.setEditable(false);
            combo.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));

        }
    }

    private static void populateTestCase() {
        try {
            TestCase testCase = new TestCase();

            if (text.getText().trim().length() > 0) {
                testCase.setFormattedID(text.getText());
            } else {
                testCase.setFormattedID(null);
            }

            if (!combo5.getText().equalsIgnoreCase("All")) {
                testCase.setType(combo5.getText());
            } else {
                testCase.setType(null);
            }

            if (!combo6.getText().equalsIgnoreCase("All")) {
                testCase.setPriority(combo6.getText());
            } else {
                testCase.setPriority(null);
            }

            RallyMain rallyMain = new RallyMain();

            if (lstTestCase != null) {
                lstTestCase.clear();
            }

            lstTestCase = rallyMain.searchObject(testCase, "TestCase",
                    minTestCaseVal, maxTestCaseVal);
            table.removeAll();

            if ((lstTestCase != null) && (lstTestCase.size() > 0)) {
                for (int i = 0; i < lstTestCase.size(); i++) {
                    TableItem tableItem = new TableItem(table, SWT.NONE);

                    TestCase task1 = (TestCase) lstTestCase.get(i);
                    String[] strValue = {
                            task1.getFormattedID(), task1.getName(),
                            task1.getType(), task1.getOwner()
                        };
                    tableItem.setText(strValue);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
